//
//  UgcMain_FirstOnePic_Cell_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/18.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "UgcMain_FirstOnePic_Cell_WX.h"
#import "UgcMain_VC_WX.h"
#import "Ugc_HomePage_VC_V3.h"
//#import "sdwebimage"
#import "TopicDetailViewController.h"

@implementation UgcMain_FirstOnePic_Cell_WX

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.viewBorder.backgroundColor = [UIColor whiteColor];
    [self.viewBorder setCornerRadiusWithRadius:10 isMasks:YES];

    [self.viewBorder mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.bottom.equalTo(self).offset(0);
    }];
    
    [self setUI];
    [self setMasonry];
    self.userInteractionEnabled = YES;
    self.backgroundColor = HexColor(0xf5f5f5);
}

-(void)setModelSub:(CoverCreateListModelSub *)modelSub{
    _modelSub = modelSub;
    UgcMainListModel *model = [[UgcMainListModel alloc]init];
    model.avatar = modelSub.avatar;
    model.nickName = modelSub.nickName;
//    model.createTime = modelSub.createTime;
    model.laudStatus = modelSub.laudStatus;
    model.laudCount = modelSub.laudCount;
    model.commentCount = modelSub.commentCount;
    model.ugcContent = modelSub.ugcContent;
    model.userId = modelSub.userId;
    model.fileList = modelSub.ugcFileList;
    model.ugcId = modelSub.relId;
    model.userSubjectTitle = modelSub.userSubjectTitle;
    self.model = model;
}


-(void)setModel:(UgcMainListModel *)model{
    
    _model = model;
    NSString *str = [NSString stringWithFormat:@"%@%@/zip",RootUrlImage,model.avatar];
    //加载图片
    NSLog(@"加载图片宽和高");
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    
    self.userName.text = model.nickName;
    
    [self changeLike:![model.laudStatus isEqualToString:@"0"]];

//    [self.like setTitle:[NSString stringWithFormat:@" %@",model.laudCount] forState:UIControlStateNormal];
    
    self.comment.text = [NSString stringWithFormat:@"%@人已赞 · %@条评论",model.laudCount,model.commentCount];

    self.usercomment.text = model.ugcContent;
    
    [self setPic];
    
    if (model.userSubjectTitle.length>0 ) {
        self.TopicLab.text = [NSString stringWithFormat:@"    %@",model.userSubjectTitle];
        self.TopicIcon.hidden = NO;
        [self.TopicLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userImage.mas_bottom).offset(10);
        }];
    }else{
        self.TopicIcon.hidden = YES;
        self.TopicLab.text = @"";
        [self.TopicLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userImage.mas_bottom).offset(0);
        }];
    }
    
    [self layoutIfNeeded];
    
}

//单张图 大小+缓存
-(void)setPic{
    NSString *pic = [NSString stringWithFormat:@"%@%@/one",RootUrlImage,self.model.fileList.firstObject];
    
    NSString *picH = [NSString stringWithFormat:@"%@H",pic];
    NSString *picW = [NSString stringWithFormat:@"%@W",pic];
    
    if (![[NSUserDefaults standardUserDefaults] objectForKey:picH]) {
        [self.PicView sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            CGSize MaxSize = CGSizeMake(kScreenW-60,(kScreenW-60)/3.0*2);
            //            UIImage *imgaeSize = [UIImage imageCompressFitSizeScale:image targetSize:MaxSize];
            
            CGSize Size = [UIImage getSingleSize:image.size MaxH:MaxSize.height MaxW:MaxSize.width url:pic];
            NSLog(@"%@",NSStringFromCGSize(Size));
            [self.PicView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(Size.height);
                make.width.mas_equalTo(Size.width);
            }];

            [self layoutIfNeeded];
            
        
        }];
        
    }else{
        
        NSString * strW = [[NSUserDefaults standardUserDefaults] objectForKey:picW];
        NSString * strH = [[NSUserDefaults standardUserDefaults] objectForKey:picH];
        CGFloat fW = [strW floatValue];
        CGFloat fH = [strH floatValue];
        
        CGSize Size = CGSizeMake(fW,fH);
        [self.PicView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(Size.height);
            make.width.mas_equalTo(Size.width);
        }];

        [self.PicView sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    }
    
}


-(void)setMasonry{
    
    self.userImage.backgroundColor = [UIColor randomColor];
    [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(30);
        make.left.equalTo(self).offset(30);
        make.width.height.mas_equalTo(30);
        
    }];
    
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.userImage.mas_centerY).offset(0);
        make.left.equalTo(self.userImage.mas_right).offset(10);
        make.height.mas_equalTo(15);
    }];
    

    [self.usercomment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TopicLab.mas_bottom).offset(10);
        make.left.equalTo(self.userImage.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(-30);
        
    }];
    
    [self.PicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.usercomment.mas_bottom).offset(10);
        make.left.equalTo(self.userImage).offset(0);
        //        make.right.equalTo(self.mas_right).offset(-20);
        make.height.mas_equalTo((kScreenW-90)/3.0*2);
        make.width.mas_equalTo(kScreenW-90);
    }];
    
    //    self.like.backgroundColor = [UIColor randomColor];
    [self.like mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PicView.mas_bottom).offset(12);
        make.right.equalTo(self.mas_right).offset(-30);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
    }];
    
    //    self.comment.backgroundColor = [UIColor randomColor];
    [self.comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PicView.mas_bottom).offset(15);
        make.left.equalTo(self.userImage).offset(0);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(self).offset(-20);
    }];

    [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userImage.mas_bottom).offset(10);
        make.left.equalTo(self.userImage).offset(0);
        make.right.equalTo(self.mas_right).offset(-30);
    }];
    
    [self.TopicIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TopicLab.mas_top).offset(5);
        make.left.equalTo(self.TopicLab.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(10, 13));
    }];
    
}


-(void)setUI{
    [self addSubview:self.userImage];
    [self addSubview:self.userName];
//    [self addSubview:self.time];
    [self addSubview:self.like];
    [self addSubview:self.comment];
    [self addSubview:self.usercomment];
    [self addSubview:self.TopicLab];
    [self addSubview:self.TopicIcon];
    [self addSubview:self.PicView];
}


-(UIImageView *)userImage{
    if (!_userImage) {
        _userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_userImage setCornerRadiusWithRadius:15 isMasks:YES];
        //        _userImage.contentMode = 2;
        _userImage.contentMode = UIViewContentModeScaleAspectFill;
        _userImage.contentScaleFactor = [[UIScreen mainScreen] scale];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.model.nickName;
            vc.userId = self.model.userId;
            vc.userAvatar =  self.model.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        
        [_userImage addGestureRecognizer:tap];
        _userImage.userInteractionEnabled = YES;
    }
    return _userImage;
}

-(QMUILabel *)userName{
    if (!_userName) {
        _userName = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _userName.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.model.nickName;
            vc.userId = self.model.userId;
            vc.userAvatar =  self.model.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        [_userName addGestureRecognizer:tap];
        _userName.userInteractionEnabled = YES;
    }
    return _userName;
}

//-(QMUILabel *)time{
//    if (!_time) {
//        _time = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        _time.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
//        _time.textColor = baseColorMedium;
//    }
//    return _time;
//}


-(BaseBtn *)like{
    if (!_like) {
        _like = [[BaseBtn alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_like setImage:[UIImage imageNamed:@"Home_SegCtrl_tracking_active"] forState:UIControlStateNormal];
        [_like setImage:[UIImage imageNamed:@"Home_SegCtrl_tracking_active_p"] forState:UIControlStateSelected];
        //        [_like setTitleColor:baseColorTheme forState:UIControlStateSelected];
        //        [_like setTitleColor:[UIColor colorWithHex:@"999999"] forState:UIControlStateNormal];
        //        _like.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;// 水平左对齐
        //        _like.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        __weak __typeof(self)weakSelf = self;
        @weakify(self);
        [[_like rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (!weakSelf.like.selected) {
                [[serviceDefualt loadUpdateUserLaud:self.model.ugcId laudType:@"1" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"点赞成功"];
                        self.model.laudStatus = @"1";
                        self.model.laudCount =  [NSString stringWithFormat:@" %ld",[self.model.laudCount integerValue]+1];
                        self.comment.text = [NSString stringWithFormat:@"%@人已赞 · %@条评论",self.model.laudCount,self.model.commentCount];
                        
                        //                        [self.like setTitle:self.model.laudCount forState:UIControlStateNormal];
                        [self changeLike:YES];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
                            //                            [Tools pushSinge];
                        }
                        self.model.laudStatus = @"0";
                        [self changeLike:NO];
                    }
                }];
            } else {
                @weakify(self);
                [[serviceDefualt loadUpdateUserLaud:self.model.ugcId laudType:@"2" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    @strongify(self);
                    if (x.success) {
                        [Tools showTips:@"取消成功"];
                        self.model.laudStatus = @"0";
                        self.model.laudCount =  [NSString stringWithFormat:@" %d",[self.model.laudCount integerValue]-1];
                        self.comment.text = [NSString stringWithFormat:@"%@人已赞 · %@条评论",self.model.laudCount,self.model.commentCount];
                        
                        //                        [self.like setTitle:self.model.laudCount forState:UIControlStateNormal];
                        [self changeLike:NO];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
                            //                            [Tools pushSinge];
                        }
                        self.model.laudStatus = @"1";
                        [self changeLike:YES];
                    }
                }];
            }
            weakSelf.like.selected = !weakSelf.like.selected;
            
        }];
    }
    return _like;
}
-(void)changeLike:(BOOL)type{
    
    if (type) {
        self.like.selected = YES;
    }else{
        self.like.selected = NO;
    }
}

-(BaseLab *)comment{
    if (!_comment) {
        _comment = [BaseLab initCustomLab:@"0人已赞 · 0条评论" color:HexColor(0x999999) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:12]];
//        [_comment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [_comment setImage:[UIImage imageNamed:@"ucg_btn_comment"] forState:UIControlStateNormal];
        //        _comment.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
//        _comment.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
//        [_comment setTitleColor:baseColorTheme forState:UIControlStateSelected];
//        [_comment setTitleColor:[UIColor colorWithHex:@"999999"] forState:UIControlStateNormal];
        _comment.userInteractionEnabled = NO;
    }
    return _comment;
}

-(QMUILabel *)usercomment{
    if (!_usercomment) {
        _usercomment = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _usercomment.numberOfLines = 0;
        _usercomment.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
        _usercomment.textColor = HexColor(0x333333);
    }
    return _usercomment;
}


-(BaseImageV *)PicView{
    if (!_PicView) {
        _PicView = [BaseImageV initCustom:CGRectMake(0, 0, 0, 0) contentMode:2];
        _PicView.clipsToBounds = YES;
        _PicView.contentMode = 0;
        [_PicView addBigImage];
//        [_PicView showBigImage:100];
        _PicView.userInteractionEnabled = YES;
    }
    return _PicView;
}




-(BaseLab *)TopicLab{
    if (!_TopicLab) {
        _TopicLab = [BaseLab initCustomLab:@"     1232131" color:HexColor(0x333333) font:[UIFont fontWithName:@"PingFangSC-Semibold" size:15]];
        _TopicLab.numberOfLines = 2;
        _TopicLab.lineBreakMode = NSLineBreakByCharWrapping;
        [[_TopicLab addTag]subscribeNext:^(id  _Nullable x) {
            TopicDetailViewController * vc = [[TopicDetailViewController
                                               alloc]init];
            vc.topicId = self.model.userSubjectId;
            [self.getNav pushViewController:vc animated:YES];
        }];
    }
    return _TopicLab;
}

-(BaseImageV *)TopicIcon{
    if (!_TopicIcon) {
        _TopicIcon = [[BaseImageV alloc]init];
        
        _TopicIcon.image = [UIImage imageNamed:@"发布_#话题"];
        _TopicIcon.backgroundColor = [UIColor clearColor];
    }
    return _TopicIcon;
}



-(BaseImageV *)iconFirst{
    if (!_iconFirst) {
        _iconFirst = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:UIViewContentModeCenter];
        _iconFirst.backgroundColor = [UIColor whiteColor];
        [_iconFirst setCornerRadiusWithRadius:4 isMasks:YES];
        _iconFirst.image = [_iconFirst scaleToSize:[UIImage imageNamed:@"动态_置顶"] size:CGSizeMake(15, 15)];
        //        _iconFirst.image = [UIImage imageNamed:@"动态_置顶"];
        
    }
    return _iconFirst;
}
@end
